import time
import hashlib
import urllib.parse
import requests
from decimal import Decimal
import base64
import re
def main(arg1: str) -> dict:
    response_text = submit(arg1)
    return {
        "result": response_text
    }

def get_key_and_value_str(params):
    """
    获得k1=v1&k2=v2 字符串
    :param params: 字典参数
    :return: 拼接后的字符串
    """
    try:
        # 按照key的ASCII码从小到大排序（字典序）
        sorted_items = sorted(params.items(), key=lambda x: x[0])

        # 构造签名键值对的格式
        param_str = ""
        for key, value in sorted_items:
            if key is not None and key != "":
                if value is not None and value != "":
                    param_str += f"{key}={value}&"

        # 去掉最后一个&
        return param_str[:-1]
    except Exception as e:
        return None


def submit(amt):
    params = {}

    # 系统参数
    params["time"] = int(time.time())
    params["uid"] = "516411"
    params["key"] = "f8d08de2a84bee5f8da30a09d77c8d832f201f1f5c6089c6ceb7086dd22d6447"

    # 业务参数
    params["mod"] = "api"
    params["type"] = "alipay"
    params["money"] = Decimal(amt)
    params["third_trade_no"] = str(params["uid"]) + str(int(time.time() * 1000))
    params["trade_name"] = "cash register"
    params["notify_url"] = "https://www.test.com/notify"
    params["return_url"] = "https://www.test.com/return"
    params["remarks"] = "SF_Express"

    sign_str = get_key_and_value_str(params)
    print(f"\nparam:{sign_str}")

    # 生成签名并移除key
    params["sign"] = hashlib.md5(sign_str.encode('utf-8')).hexdigest()
    params.pop("key", None)

    # 发送请求
    # response = requests.get("https://www.cuupay.com/" + "submit", params=params)
    # 构建完整URL并返回参数
    base_url = "https://www.cuupay.com/submit"
    encoded_params = urllib.parse.urlencode(params)
    full_url = f"{base_url}?{encoded_params}"

    return full_url
